package com.huike.web.controller.review;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult add(@PathVariable String name, @PathVariable Integer age, @PathVariable String sex){
        boolean add = reviewService.add(name, age, sex);
        return add? new AjaxResult(200,"成功插入:1条数据"): AjaxResult.error("插入失败");
    }
    @GetMapping("/saveData")
    public AjaxResult add1(@RequestBody Review review){
        boolean b = reviewService.add1(review);
        return b?  new  AjaxResult(200,"成功插入:1条数据"):AjaxResult.error("插入失败");
    }
    /**=========================================================新增数据============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable Long id){
       boolean del = reviewService.del(id);
       return del? new  AjaxResult(200,"成功删除"):AjaxResult.error("删除失败");
   }

    /**=========================================================删除数据=============================================*/
   @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
       boolean b = reviewService.updateByid(review);
       return b? new AjaxResult(200,"修改成功"):AjaxResult.error("修改失败");
   }

    /**=========================================================修改数据=============================================*/

    @GetMapping("/getById")
    public AjaxResult get(Long id){
        Review review = reviewService.selectById(id);
        return new AjaxResult(200,"操作成功",review);
    }


    @GetMapping("/getDataByPage")
    public AjaxResult getByPage(@Param("pageNum") int pageNum, @Param("pageSize")int pageSize){
        int count = reviewService.count();
        Map map = new HashMap<>();
        map.put("total",count);
        List<Review> reviews = reviewService.selectByPage(pageNum, pageSize);
        map.put("rows",reviews);
        return new AjaxResult(200,"查询成功",map);
    }

    /**=========================================================查询数据=============================================*/

}